package com.spark.lola.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.logistics.constant.LogisticsConstant;
import com.spark.lola.logistics.entity.LocationInfo;
import com.spark.lola.logistics.mapper.LocationInfoMapper;
import com.spark.lola.logistics.service.LocationInfoService;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.core.vo.SelectMap;
import com.spark.lola.support.mybatis.entity.PageParam;
import com.spark.lola.support.mybatis.entity.QueryParam;
import com.spark.lola.support.mybatis.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Zhi-peng wang
 * @date: 2020/04/27 17:11
 * 货运地址信息业务实现
 **/
@Service
public class LocationInfoServiceImpl extends BaseServiceImpl<LocationInfoMapper, LocationInfo> implements LocationInfoService {

    @Autowired
    private LocationInfoMapper locationInfoMapper;

    @Override
    public IPage<LocationInfo> findPage(QueryParam<LocationInfo> queryParam) {
        PageParam<LocationInfo> pageParam = queryParam.toPageParam();
        return locationInfoMapper.findPage(pageParam.getPage(), pageParam.getEntityWrapper());
    }

    @Override
    public List<LocationInfo> findLike(Integer locationType, String key) {
        QueryWrapper<LocationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*");
        queryWrapper.eq("used", CommonConstant.Y);
        if (locationType != null && locationType != -1) {
            queryWrapper.eq("location_type", locationType);
        }
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and(o -> o.like("no", key).or().like("name", key));
        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<SelectMap> selectMap(Integer type, String key) {
        QueryWrapper<LocationInfo> queryWrapper =new QueryWrapper<>();
        queryWrapper.select("id,name");
        queryWrapper.eq("location_type", type);
        queryWrapper.eq("used", CommonConstant.Y);
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and(o->o.like("no",key).or().like("name",key));
        }
        List<LocationInfo> lst = baseMapper.selectList(queryWrapper);
        List<SelectMap> selectMaps =new ArrayList<>();
        if(!CollectionUtils.isEmpty(lst)) {
            lst.forEach(item -> {
                selectMaps.add(new SelectMap(item.getId(), item.getName()));
            });
        }
        return selectMaps;
    }
}
